package me.zhengjie.modules.system.util;

import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @Author neo
 * @Date 2022-03-14 17:17
 * @email ni.bin@mingwork.com
 */
public class HttpAliUtil {

    private static OkHttpClient clientInstance;
    //非公平锁
//    private static ReentrantLock nonFairLock = new ReentrantLock();

    private static Integer connectionTimeOut = 10;

    private static Integer writeTimeOut = 10;

    private static Integer readTimeOut = 30;

    static void init(){
        //采用链接池复用http请求链接省去握手时间
        ConnectionPool pool = new ConnectionPool();

        //初始化client 实例
        clientInstance =new  OkHttpClient.Builder()
                .connectTimeout(connectionTimeOut, TimeUnit.SECONDS)
                .writeTimeout(writeTimeOut, TimeUnit.SECONDS)
                .readTimeout(readTimeOut, TimeUnit.SECONDS)
                .connectionPool(pool)
                .build();
    }

    public static synchronized OkHttpClient getClientInstanc(){
        if(Objects.isNull(clientInstance)){
            init();
        }
        return clientInstance;
    }



}
